package cn.zw.msmp.service.impl;

import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.zw.msmp.dao.TransactionDao;
import cn.zw.msmp.pojo.TransactionInfo;
import cn.zw.msmp.service.ITransactionService;

@Service("transactionService")
public class TransactionServiceImpl implements ITransactionService {

	private static final Logger log = LogManager.getLogger(TransactionServiceImpl.class);
	
	@Autowired
	private TransactionDao dao;
	
	@Override
	public TransactionInfo getTransaction(Integer id) {
		try {
			if(null != id) {
				return dao.getTransaction(id);
			}
		} catch (Exception e) {
			log.error("get transaction by id error, id="+id.intValue(), e);
		}
		return null;
	}

	@Override
	public void insert(TransactionInfo transactionInfo) {
		try {
			if(null != transactionInfo) {
				dao.insert(transactionInfo);
			}
		} catch (Exception e) {
			log.error("insert transaction record error", e);
		}
	}
	
	@Override
	public void update(TransactionInfo transactionInfo) {
		try {
			if(null != transactionInfo) {
				dao.update(transactionInfo);
			}
		} catch (Exception e) {
			log.error("update transaction record error", e);
		}
	}

	@Override
	public PageInfo<TransactionInfo> queryTransactions(TransactionInfo info, int pageIndex, int pageSize) {
		try {
			if(null != info) {
				PageHelper.startPage(pageIndex, pageSize);
				List<TransactionInfo> list = dao.queryTransactions(info);
				return new PageInfo<TransactionInfo>(list);
			}
		} catch (Exception e) {
			log.error("query transaction record error", e);
		}
		return null;
	}

	@Override
	public int delete(List<Integer> ids) {
		try {
			if(null != ids && ids.size() > 0) {
				return dao.delete(ids);
			}
		} catch (Exception e) {
			log.error("delete transaction error", e);
		}
		return 0;
	}

}
